package com.authine.cloudpivot.web.api.secondopen.dao;

import com.authine.cloudpivot.web.api.secondopen.domain.AttachModel;
import com.authine.cloudpivot.web.api.secondopen.domain.Attachment;
import com.authine.cloudpivot.web.api.secondopen.domain.NewsModel;
import com.authine.cloudpivot.web.api.secondopen.domain.NewsTypeModel;
import com.authine.cloudpivot.web.api.secondopen.domain.RemarkModel;
import com.authine.cloudpivot.web.api.secondopen.mapper.NewsSqlProvider;
import com.authine.cloudpivot.web.api.secondopen.vo.DimisstionVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface NewsMapper {

    @SelectProvider(type = NewsSqlProvider.class, method = "selectNewsList")
    List<NewsModel> selectNewsList(@Param("tableName") String tableName, @Param("newsType") String newsType, @Param("title") String title, @Param("permission") List<String> permission);

    @SelectProvider(type = NewsSqlProvider.class, method = "selectIndexNewsList")
    List<NewsModel> selectIndexNewsList(@Param("tableName") String tableName, @Param("newsType") String newsType, @Param("title") String title, @Param("permission") List<String> permission);



    @SelectProvider(type = NewsSqlProvider.class, method = "selectNewsList2")
    List<NewsModel> selectNewsList2(@Param("tableName") String tableName, @Param("newsType") String newsType, @Param("title") String title, @Param("permission") List<String> permission, @Param("isImportant") String isImportant, @Param("fromTime") String fromTime, @Param("toTime") String toTime);

    @Select("select COUNT(id) from ${tableName} where bizObjectId = #{bizObjectId}")
    int selectComentCount(@Param("tableName") String tableName, @Param("bizObjectId") String bizObjectId);

    @SelectProvider(type = NewsSqlProvider.class, method = "Count")
    int selectCount(@Param("tableName") String tableName, @Param("recordType") String recordType, @Param("bizObjectId") String bizObjectId, @Param("creater") String creater);

    @Select("select id,author,IfShow,newsType,title,content,publishTime,top,creater,permission from ${tableName} where id = #{id}")
    NewsModel selectNewsById(@Param("tableName") String tableName, @Param("id") String id);

    @Select("select id,IfShow,leftType,newsType,title,content,publishTime,top,creater,permission,LongText1591062723759 from ${tableName} where id = #{id}")
    NewsModel selectNewsById2(@Param("tableName") String tableName, @Param("id") String id);

    @Select("select id,name,refId,fileSize,mimeType from h_biz_attachment where bizObjectId = #{bizObjectId} and bizPropertyCode='attachment'")
    List<AttachModel> selectAttchByBizId(@Param("bizObjectId") String bizObjectId);

    @Select("select id,creater,content,modifiedTime,bizObjectId from ${tableName} where bizObjectId = #{bizObjectId}  and ( opinion = '通过' or creater = #{userId} ) order by modifiedTime desc")
    List<RemarkModel> selectRemarkByBizId(@Param("userId") String userId, @Param("tableName") String tableName, @Param("bizObjectId") String bizObjectId);

    //上一页-------------------------------------------下一页
    @SelectProvider(type = NewsSqlProvider.class, method = "selectTop")
    NewsModel selectTop(@Param("tableName") String tableName, @Param("newsType") String newsType, @Param("flag") String flag, @Param("newsType") Date publishTime, @Param("permission") List<String> permission, @Param("top") String top, @Param("order") String order);

    @SelectProvider(type = NewsSqlProvider.class, method = "selectNomal")
    NewsModel selectNomal(@Param("tableName") String tableName, @Param("newsType") String newsType, @Param("flag") String flag, @Param("newsType") Date publishTime, @Param("permission") List<String> permission, @Param("top") String top, @Param("order") String order);

    @SelectProvider(type = NewsSqlProvider.class, method = "selectTopMin")
    NewsModel selectTopMin(@Param("tableName") String tableName, @Param("newsType") String newsType, @Param("permission") List<String> permission, @Param("top") String top, @Param("order") String order);

    @SelectProvider(type = NewsSqlProvider.class, method = "selectNomalMax")
    NewsModel selectNomalMax(@Param("tableName") String tableName, @Param("newsType") String newsType, @Param("permission") List<String> permission, @Param("top") String top, @Param("order") String order);

    @Delete("delete FROM ${tableName}  WHERE bizObjectId = #{bizObjectId}  and modifier=#{userId} and recordType=#{type} ")
    int deleteNumber(@Param("bizObjectId") String bizObjectId, @Param("newsType") String newsType, @Param("tableName") String tableName, @Param("userId") String userId, @Param("type") String type);

    @Select("SELECT id, creater, content, bizObjectId,modifiedTime  FROM ${tableName}  WHERE bizObjectId = #{bizObjectId} and (creater=#{userId} or opinion!='' and opinion!='不通过')")
    List<RemarkModel> selectNewsRemarkList(@Param("tableName") String tableName, @Param("bizObjectId") String bizObjectId, @Param("userId") String userId);

    @Delete("delete FROM ${tableName}  WHERE id = #{id}  ")
    int deleteRemark(@Param("id") String id, @Param("tableName") String tableName);

    @Select("select id,newsType,belong from iusi6_NewsTypeManager where status = '启用' order by sort ASC")
    List<NewsTypeModel> selectNewsType();

    @SelectProvider(type = NewsSqlProvider.class, method = "selectNewsListCount")
    int selectNewsListCount(@Param("tableName") String tableName, @Param("newsType") String newsType, @Param("permission") List<String> permission);

    @Select("select COUNT(id) from ${tableName} where newsType = #{newsType} and creater = #{creater}")
    int selectReadCount(@Param("tableName") String tableName, @Param("newsType") String newsType, @Param("creater") String creater);

    @SelectProvider(type = NewsSqlProvider.class, method = "selectNewsListByDept")
    List<NewsModel> selectNewsListByDept(@Param("tableName") String tableName, @Param("newsType") String newsType, @Param("title") String title, @Param("permission") List<String> permission);

    @Select("SELECT id, creater, content, bizObjectId,modifiedTime  FROM ${tableName}  WHERE opinion = ''")
    List<RemarkModel> selectRemarkList(@Param("tableName") String tableName);

    @SelectProvider(type = NewsSqlProvider.class, method = "selectImageNewsList")
    List<NewsModel> selectImageNewsList(@Param("tableName") String tableName, @Param("newsType") String newsType, @Param("permission") List<String> permission);

    @Select("select id, name,refId from h_biz_attachment where bizObjectId = #{bizObjectId} and bizPropertyCode in ('pictrue','displayIcon')")
    List<AttachModel> selectPicByBizId(@Param("bizObjectId") String bizObjectId);

    @Select("SELECT a.refId FROM ${tableName} i LEFT JOIN h_biz_attachment a on i.id=a.bizObjectId where  i.id =#{id} and bizPropertyCode='pictrue'")
    List<Attachment> selectAttachmentByNewsId(@Param(value = "tableName") String tableName, @Param(value = "id") String id);

    @Select("select detail from middle_log_table where middle_log_table.creatTime > DATE_SUB(NOW(), INTERVAL 5 MINUTE)")
    List<String> selectMiddleList();


    @Insert("insert into dimisstion_crew(dimisstionUser) values(#{dimisstionUser})")
    Integer insertDimisstion(DimisstionVo dimisstionVo);
}
